/*
 * @Author: ChunLai
 * @Date: 2022-11-29 17:16:03
 * @LastEditTime: 2025-08-29 14:35:51
 * @Description: 其他信息校验规则(保单+险种) --- 待使用
 * @FilePath: \02.bldinsure\src\translate\validate\index.js
 */
import { getMessage } from "@/methods/cache.js";
import { createResult } from "@/translate/validate/customer.js";
export const validOtherInfo = (totalPrem) => {
  let obj = {
    msg: "",
    status: true,
    target: "",
  };
  const insert_cache = getMessage("insertParams"),
    bank_cache = insert_cache.bankInfo,
    vuex_cache = getMessage("vuex"),
    vuex_bank = vuex_cache.dictionary.bankList;
  // 银行卡限额校验
  let bankArr = vuex_bank.filter((item) =>
    bank_cache.payMode == "R" ? item.bankR : item.bank7
  );
  const fdBankObj = bankArr.find((ele) => ele.keyId === bank_cache.bankCode);
  if (fdBankObj) {
    const get_obj =
      bank_cache.payMode == "R" ? fdBankObj.limitR : fdBankObj.limitB;
    if (get_obj || !["无限额"].includes(get_obj)) {
      if (totalPrem * 1 >= get_obj) {
        const get_val =
            bank_cache.payMode == "R" ? fdBankObj.small : fdBankObj.batch,
          msg2 = get_val.indexOf("元") !== -1 ? get_val : `${get_val}元`;
        // obj.msg = `首期保费超过${fdBankObj.text}限额${msg2},请确认`;
        obj.msg = `首期保费（${totalPrem}）已超过所选银行的限额，请更换银行卡或者选择线下批扣`;
        obj.status = false;
        return obj;
      }
    }
  }

  return obj;
};

export const validRisksInfo = (risksInfo, totalPrem) => {
  // console.log("validRisksInfo", risksInfo)
  const mainRisk = risksInfo[0],
    riskLen = risksInfo.length;
  let premCheck = true, // 保费校验标识
    proName = "",
    obj = {
      msg: "",
      status: true,
    };
  for (var i = 0; i < riskLen; i++) {
    const item = risksInfo[i];
    if (item.prem <= 0) {
      premCheck = false;
      proName = item.productName;
      break;
    }
  }
  // 保费不能为0校验
  if (!premCheck) {
    obj.msg = `'${proName}'险种不符合投保规则,请确认`;
    obj.status = false;
    return obj;
  }

  /* ======= 核保特殊校验规则 ======= */
  if (mainRisk.rules) {
    const findCheck = mainRisk.rules.find(
      (ele) => ele.type === "underwritingBeforeLimit"
    );
    if (findCheck) {
      if (findCheck.totalMinPrem && totalPrem * 1 < findCheck.totalMinPrem) {
        obj.msg = `不符合投保规则: 总保费最低${findCheck.totalMinPrem}元`;
        obj.status = false;
        return obj;
      } else if (
        findCheck.totalMaxPrem &&
        totalPrem * 1 > findCheck.totalMaxPrem
      ) {
        obj.msg = `不符合投保规则: 总保费最高${findCheck.totalMaxPrem}元`;
        obj.status = false;
        return obj;
      }
      // 保额算保费的主险,校验保费
      else if (findCheck.minPrem && mainRisk.prem * 1 < findCheck.minPrem) {
        obj.msg = `不符合投保规则: 主险最低保费${findCheck.minPrem}元`;
        obj.status = false;
        return obj;
      } else if (findCheck.maxPrem && mainRisk.prem * 1 > findCheck.maxPrem) {
        obj.msg = `不符合投保规则: 主险最高保费${findCheck.minPrem}元`;
        obj.status = false;
        return obj;
      }
    }
    // 第二规则(万能险)
    const find2Check = mainRisk.rules.find((ele) => ele.type === "omnipotent");
    if (find2Check) {
      const relateBill = getMessage("relaDatas") || {},
        tgrObj = relateBill.serialNumber
          ? find2Check.relate
          : find2Check.single;
      if (tgrObj.minPrem && totalPrem * 1 < tgrObj.minPrem) {
        obj.msg = `不符合投保规则: ${mainRisk.productName}${tgrObj.err}`;
        obj.status = false;
        return obj;
      }
    }
  }
  return obj;
};

export const userRelateRisk = (mainInsu, bnfCategory = "0") => {
  // console.log("userRelateRisk");
  const chkObj = mainInsu.dutyLimitBnfCategory;
  // 仅针对计划4、计划7-9、计划13-16支持法定、指定受益人，传值核心时受益人类型为身故受益人；其他计划均不支持选择受益人
  if (chkObj && chkObj.bnfValue == bnfCategory) {
    let fd3Obj = mainInsu.dutyList.find((e) => e.value == chkObj.dutyValue);
    if (!fd3Obj) {
      fd3Obj = mainInsu.dutyList.find((e) => {
        const obj = e.radioOptions.find((r) => r.value == chkObj.dutyValue);
        if (obj) {
          return e;
        }
      });
      return createResult(
        false,
        `未选择“${fd3Obj.label}”责任，不支持选择受益人`,
        ""
      );
    }
  }
  const chk2Obj = mainInsu.planLimitBnfCategory;
  if (chk2Obj && chk2Obj.bnfValue == bnfCategory) {
    const parr = chk2Obj.planValue.split("-");
    const checked = parr.includes(mainInsu.planCode);
    if (checked) {
      const plan2Obj = mainInsu.plansArr.find(
        (e) => e.value == mainInsu.planCode
      );
      const label2 = `${plan2Obj.label}（${plan2Obj.content}）`;
      return createResult(false, `${label2}：${chk2Obj.toast}`, "");
    }
  }

  return createResult(true);
};
